// pages/travel/travel.js

var amapFile = require('../../libs/amap-wx.js');
let amap = require("../../libs/amap.js");

var myAmapFun = new amapFile.AMapWX({

  key: '1336bfc7fd9f11ec452d1d4f0cab4c8c' // 必填 换成自己申请到的

});

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue: '',
    placeHould: '查找地点、公交、地铁',
    jd: null,
    wd: null,
    markers: [],
    map_h: 'calc(100vh - 105rpx)',
    points: [],
    textData: {},

    showModel:false,
    menuList:[{
      text:'景点',
      icon:'tourism2',
      name:'scenic'
    },{
      text: '餐饮',
      icon: 'food1',
      name: 'restaurant'
    },{
      text: '住宿',
      icon: 'building',
      name: 'accommodation'
    },{
      text: '购物',
      icon: 'shopcartfull',
      name: 'shopping'
    },{
      text: '娱乐',
      icon: 'entertainment',
      name: 'entertainment'
    },{
      text: '出行',
      icon: 'list_travel_location',
      name:'travel'
    },{
      text: '取消',
      icon: 'cancle',
      name:'cancle'
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        console.log(res.latitude, res.longitude, '当前位置经纬度')
        var latitude1 = res.latitude;
        var longitude1 = res.longitude;
        var mapcenter = [latitude1, longitude1];

        that.setData({
          wd: latitude1,
          jd: longitude1,
          points: [{
            longitude: res.longitude,
            latitude: res.latitude
          }],

        })
      }
    })
  },
  makertap: function (e) {
    console.log(e.markerId, e, '点击气泡');
    var that = this;

    var id = e.markerId;
    var markersData = that.data.markers;
    that.showMarkerInfo(markersData, id);

    that.setData({
      map_h: 'calc(100vh - 105rpx)'
    })
  },
  showMarkerInfo: function (data, id) {

    var that = this;

    data.map((track) => {
      if (track.id == id) {
        console.log(track);

        that.setData({
          select_place: track,
          textData: {
            name: track.name,
            desc: track.address
          }
        });
      }
    })

  },
  mapchange: function (e) {
    console.log(e, 'mapchange拖拽地图触发')
  },
  suo_result: function () {
    console.log('搜索结果');

    let { wd, jd, city } = this.data;
    wx.navigateTo({
      url: `../storesearch/storesearch?city=${city}&lonlat=${jd},${wd}`,
    })

  },
  goDetail: function () {
    var that = this;
    if (that.data.select_place == undefined) return;

    wx.openLocation({
      latitude: parseFloat(that.data.select_place.latitude),
      longitude: parseFloat(that.data.select_place.longitude),
      name: that.data.select_place.name,
      scale: 28
    })
  },

  onBindChange: function (e) {
    var that = this;

    console.log(e.detail.value);
    var v = e.detail.value;

    that.getMarksLocation(v);
  },

  searchStyle:function(e){
    console.log(e);
    var name = e.currentTarget.dataset.name;

    this.hideModal();
    

    var key;
    switch (name){
      case 'scenic':
        key = '景点';
        break;
      case 'restaurant':
        key = '餐饮';
        break;
      case 'accommodation':
        key = '住宿';
        break;
      case 'shopping':
        key = '购物';
        break;
      case 'entertainment':
        key = '娱乐';
        break;    
      case 'travel':
        key = '出行';
        break;
      case 'cancle':
        return;
    }
    this.getMarksLocation(key);
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    /**/
    that.getMarksLocation();
  },
  getMarksLocation: function (keywords) {
    var that = this;

    var k = keywords || '';
    myAmapFun.getPoiAround({
      querykeywords: k,
      success: function (data) {
        //成功回调
        console.log(data, 'success');

        var markers = data.markers;
        var poisData = data.poisData;
        var city = poisData[0].pname + poisData[0].cityname;
        var mark = [];

        for (var i = 0; i < markers.length; i++) {
          mark[i] = {
            iconPath: "../../images/position.png",
            id: poisData[i].id,
            latitude: markers[i].latitude,
            longitude: markers[i].longitude,
            width: 20,
            height: 20,
            name: markers[i].name,
            address: markers[i].address
          }
        }
        that.setData({
          markers: mark,
          city: city
        })

      },
      fail: function (info) {
        //失败回调
        console.log(info, 'fail');
        wx.showToast({
          title: info.errMsg
        });
      }
    })
  },
  /*
   *打开种类弹框
   */ 
  showKinds:function(){
    var that = this;

    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModel: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModel: true
      })
    }.bind(this), 200)

  },
  /*
   *关闭种类弹框
   */
  hideModal:function(){
    var that = this;

    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModel: false
      })
    }.bind(this), 200)

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})

// https://blog.csdn.net/eadio/article/details/78920196 搜索周边poi接口实践